Ext.define('BenBen.modules.customer.CustomerModule',{
    extend : 'Ext.ux.desktop.Module',
    requires: [
        'BenBen.common.BasePanel',
        'BenBen.modules.customer.CustomerForm'
    ],
    id : 'customer-win',
    border : false,
    init : function(){
        this.launcher = {
            text: '客户管理',
            iconCls:'icon-grid'
        };
    },
    createWindow : function() {
        var desktop = this.app.getDesktop();
        var win = desktop.getWindow('customer-win');

        if (!win) {
            var dataModelFields = ['_id','customerName', 'customerNo', 'grade','province','city'];

            var columns = [{
                xtype: 'rownumberer',
                width: 40,
                sortable: false,
                locked: true
            },{
                dataIndex: 'customerName',
                text: '客户名称'
            },{
                dataIndex: 'customerNo',
                text: '客户编号'
            },{
                dataIndex: 'province',
                text: '省'
            },{
                dataIndex: 'city',
                text: '市'
            },{
                dataIndex: 'grade',
                text: '客户级别',
                renderer: function (value) {
                    if (value == '1') {
                        return "潜在客户";
                    }else if (value == '2') {
                        return "普通客户";
                    } else if (value == '3') {
                        return "主要客户";
                    } else if (value == '4') {
                        return "大客户";
                    }
                }
            }];

            var searchItem = [{
                xtype: 'textfield',
                name: 'customerName',
                isSearch: true,
                fieldLabel: '客户名称',
                labelAlign: 'right'
            },{
                xtype: 'textfield',
                name: 'customerNo',
                isSearch: true,
                fieldLabel: '客户编号',
                labelAlign: 'right'
            }];

            Ext.define('model.customer', {
                extend: 'Ext.data.Model',
                fields: [{
                    name: 'customerName'
                },{
                    name: 'upCustomer'
                },{
                    name: 'type'
                },{
                    name: 'createTime'
                },{
                    name: 'remark'
                },{
                    name: 'status'
                },{
                    name: 'customerNo'
                }]
            });

            var dataForm = Ext.create('BenBen.modules.customer.CustomerForm', {
                reader :  Ext.create('Ext.data.JsonReader', {
                    root : 'data',
                    model : 'model.customer'
                })
            });

            var basePanel = Ext.create('BenBen.common.BasePanel', {
                columns : columns,
                searchItem : searchItem,
                dataForm : dataForm,
                dataModelFields : dataModelFields,
                tbarDefaultItems : 'add,update',
                tbarItems : [],
                config : {
                    url : '/customer/customers',
                    pageSize : 30,
                    winWidth : 800,
                    winHeigth : 480
                }
            });
            win = desktop.createWindow({
                id: 'customer-win',
                title: '客户管理',
                width: 800,
                height: 480,
                iconCls: 'icon-grid',
                animCollapse: false,
                constrainHeader: true,
                layout: 'fit',
                items: [basePanel]
            });
        }


        return win;
	}
});
